// pages/make/make.js
const app = getApp()
Page({

  /**
   * 页面的初始数据 
   */
  data: { 
    arrays:[],
    showModal: false, 
    selectCourse:'', 
    items: []
  },

  handleSee: function() {
    wx.navigateTo({
      url: '/pages/make-detial/make-detial',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;
  //判断是否绑定了孩子
    wx.getStorage({
      key: 'childcode',
      success: function (res) {
        console.log('拿到缓存中的childcode')
        console.log(res.data)
        var childcode = res.data;
          //请求接口，获取签到信息
          var serverUrl = app.serverUrl;
          var arrays = [];
          var items = [];
          var newarray;
          console.log('要传给后台的childcode' + childcode);
          var childcode = childcode;
          console.log(childcode);
          wx.request({
            url: serverUrl + '/getInParentSign',
            data: {
              childcode: childcode,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            method: "POST",
            success: function (res) {
              console.log(res.data)

              //获取每一门课程的课程情况
              var physicFitnessInfo = res.data.data.physicFitness;
              var jumpRopeInfo = res.data.data.jumpRope;
              var swimInfo = res.data.data.swim;
              var midTextInfo = res.data.data.midText;
              var badmintonInfo = res.data.data.badminton;


              //存储体适能课程信息
              if (physicFitnessInfo != null){
                items = items.concat('体适能');
                newarray = [{
                  classname: '体适能',
                  totalclass: physicFitnessInfo.classsum,
                  restclass: physicFitnessInfo.count
                }]
                arrays = arrays.concat(newarray);
              }
              

              //存储花式跳绳课程信息
              if (jumpRopeInfo != null){
                items = items.concat('花式跳绳');
                newarray = [{
                  classname: '花式跳绳',
                  totalclass: jumpRopeInfo.classsum,
                  restclass: jumpRopeInfo.count
                }]
                arrays = arrays.concat(newarray);
              }
              //存储游泳课程信息
              if (swimInfo != null){
                items = items.concat('游泳');
                newarray = [{
                  classname: '游泳',
                  totalclass: swimInfo.classsum,
                  restclass: swimInfo.count
                }]
                arrays = arrays.concat(newarray);
              }

              //存储中考体育课程信息
              if (midTextInfo != null){
                items = items.concat('中考体育');
                newarray = [{
                  classname: '中考体育',
                  totalclass: midTextInfo.classsum,
                  restclass: midTextInfo.count
                }]
                arrays = arrays.concat(newarray);
              }

              //存储羽毛球课程信息
              if (badmintonInfo != null){
                items = items.concat('羽毛球');
                newarray = [{
                  classname: '羽毛球',
                  totalclass: badmintonInfo.classsum,
                  restclass: badmintonInfo.count
                }]
                arrays = arrays.concat(newarray);
              }

              me.setData({
                arrays: arrays,
                items:items
              })

            }
          })
        }
    })   

  },

  showDialogBtn: function() {
    this.setData({
      showModal: true
    })
  },
  hideModal: function() {
    this.setData({
      showModal: false
    });
  },
  /**
   * onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function(e) {
    this.hideModal();
    var me = this;
    wx.getStorage({
      key: 'childcode',
      success: function(res) {
        console.log('拿到缓存中的childcode')
        console.log(res.data)
        var childcode = res.data;

        var serverUrl = app.serverUrl;

        var selectCourse = me.data.selectCourse;
        var arrays = me.data.arrays;

        console.log('llll' + selectCourse)
        console.log(arrays[0].restclass)
        wx.request({
          url: serverUrl + '/doParentSign',

          data: {
            childCode: childcode,
            className: selectCourse
          },
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log('\\\\\\\\')
            console.log(res.data);

            if (res.data.status == 200) {
              //更新页面数据
              console.log(selectCourse)

              for (var i = 0; i < arrays.length; i++) {
                if (arrays[i].classname == selectCourse) {
                  arrays[i].restclass = arrays[i].restclass - 1;
                }
              }

              me.setData({
                arrays: arrays
              })
              wx.showToast({
                title: '签到成功',
              })
            } else if (res.data.status == 500) {
              wx.showToast({
                title: res.data.msg,
              })
            }
          }

        })
      },
    })


  },
  
  onCancel: function() {
    this.hideModal();

  },

  radioChange(e) {
    var me = this;
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var selectCourse = e.detail.value;
    
    me.setData({
      selectCourse: selectCourse
    })
  },


  // 隐藏弹窗



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})